package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.sh.response.PageResult;
import com.itheima.sh.response.ResponseResult;
import com.itheima.sh.response.StatusCode;
import com.itheima.sh.pojo.User;
import com.itheima.sh.service.UserService;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Dqh
 * @create 2020-09-17 21:08
 * @desc
 **/
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * @desc: 查询所有用户
    * @param
     * @return:
     * @author: Dqh  2020/9/17  21:43
     */
    @GetMapping
    public ResponseResult<User> findAll(){
        //int i= 1/0;
        List<User> user = userService.findAll();
        return ResponseResult.okResult(user);
    }

    /**
     * @desc: 新增用户
    * @param user
     * @return:
     * @author: Dqh  2020/9/17  21:35
     */
    @PostMapping
    public ResponseResult<User> add(@RequestBody User user){
        return ResponseResult.okResult(userService.add(user));
    }

    /**
     * @desc: 根据uuid删除用户
     * @param uuid
     * @return:
     * @author: Dqh  2020/9/17  21:58
     */
    @PostMapping("delete")
    public ResponseResult<User> deleteUser(@RequestParam Integer uuid){
        boolean b = userService.deleteUser(uuid);
        return new ResponseResult(b ? StatusCode.SUCCESS.getCode() : StatusCode.ERROR.getCode(),
                b ? "删除成功" : "删除失败");
    }

    /**
     * @desc:查询单个用户信息
    * @param uuid
     * @return:
     * @author: Dqh  2020/9/17  23:14
     */
    @GetMapping("/{id}")
    public ResponseResult<User> findOne(@PathVariable("id") Integer uuid) {
        User user = userService.findOne(uuid);
        return new ResponseResult<User>(user!=null ? StatusCode.SUCCESS.getCode() : StatusCode.ERROR.getCode(),
                user!=null ? "查询成功" : "查询失败", user);
    }

    /**
     * @desc: 修改用户
    * @param user
     * @return:
     * @author: Dqh  2020/9/17  23:14
     */
    @PutMapping
    public ResponseResult<User> update(@RequestBody User user) {
        boolean b = userService.update(user);
        return new ResponseResult<User>(b ? StatusCode.SUCCESS.getCode() : StatusCode.ERROR.getCode(),
                b ? "修改成功" : "修改失败");
    }
    /**
     * @desc: 带条件分页查询
    * @param user
     * @param page
     * @param size
     * @return:
     * @author: Dqh  2020/9/17  23:14
     */
    @GetMapping("/search/{page}/{size}")
    public ResponseResult<User> search(@RequestBody User user, 	@PathVariable("page") Integer page,
                               @PathVariable("size") Integer size){
        Page<User> pages = userService.search(user,page, size);
        // 数据列表，总记录，总页数
        PageResult pageResult =
                new PageResult(pages.getTotal(),pages.getPages(),pages.getResult());
        return new ResponseResult(StatusCode.SUCCESS.getCode(), "查询成功", pageResult);
    }


}
